package Game;
import java.io.*;
import java.util.*;


/**
 * Read in file and the return values as requested
 * 
 * @author Ryan Goodrich
 * @version 1.0
 */
public class FileInput implements Serializable{

	private ArrayList<HashMap<String, String>> fileData;
	
	/**
	 * Parse the file and add all data to the structure
	 * 
	 * @param filePath File path to be used to input file
	 */
	public FileInput(String filePath) {
		fileData = new ArrayList<HashMap<String, String>>();
		String line = null;
		String key, value;
		
		try {
			// read in file
			BufferedReader fileReader = new BufferedReader(new FileReader(filePath)); 

			// get the first line of the file
			line = fileReader.readLine();
			
			// set starting array list index
			boolean inItem = false;
			
			// create hash map object to use for inserts
			HashMap<String, String> itemInformation = null;
			
			// read until end of file
			while (line != null) {
				
				// check if line is comment or blank
				if (!line.equals("") && line.charAt(0) != '|') {
					
					// check if start or end of string
					if (line.indexOf("====================") != -1) {
						if (!inItem)
							itemInformation = new HashMap<String, String>();
						else 
							fileData.add(itemInformation);
						
						// switch between in and out of item
						inItem = !inItem;
					}
					
					// store data if in an item only if its a value
					if (inItem && line.indexOf(':') != -1) {
						key = line.substring(0, line.indexOf(':')).trim();
						value = line.substring(line.indexOf(':') + 2).trim();
						if (itemInformation.containsKey(key))
							value = itemInformation.get(key) + "|_|" + value;
						itemInformation.put(key, value);
					}
				}
				
				// read in the next line
				line = fileReader.readLine();
			}
		
		}
		catch (FileNotFoundException e) {
			System.err.println("Could not find file: " + filePath + " in BookList::populateBooksFromFile");
			e.printStackTrace();
		}
		catch (IOException e) {
			System.err.println("Line value was: " + line);		
			e.printStackTrace();
		}
	}
	
	/**
	 * Get the total number of unique items
	 * 
	 * @return Size of list
	 */
	public int size() {
		return fileData.size();
	}
	
	/**
	 * Check if the index is greater then -1 and less then the total
	 * size of all the unique items in the list
	 * 
	 * @param index Index to be checked
	 * @return True if the value is valid, False otherwise
	 */
	public boolean checkIndex(int index) {
		return index > -1 && index < size();
	}
	
	/**
	 * Get the raw data store for a record
	 * 
	 * @param index Index of the record searching for
	 * @return A the record for the requested index, Null if the index is not valid
	 */
	public HashMap<String, String> getRawDataAt(int index) {
		if (!checkIndex(index))
			return null;
		return fileData.get(index);
	}
	
	/**
	 * Find the index of a key/ value pair
	 * 
	 * @param key The key to be searching for
	 * @param value The value to be searching for
	 * @return The index of the first occurrence of the key/ value pair 
	 */
	public int getIdOf(String key, String value) {
		for (int i = 0; i < fileData.size(); i++)
			if (fileData.get(i).containsKey(key) && fileData.get(i).get(key).equals(value))
				return i;
		return -1;
	}
	
	/**
	 * Return an array of all the indexes where the key/ value pair is present
	 * 
	 * @param key The key value to be searching for
	 * @param value The value that the key should be. If NULL it will return 
	 * 		all indexes containing the key
	 * @return All indexes where the key/ value is found
	 */
	public ArrayList<Integer> getAllIdsWhere(String key, String value) {
		ArrayList<Integer> returnList = new ArrayList<Integer>();
		for (int i = 0; i < fileData.size(); i++)
			if (fileData.get(i).containsKey(key) && (value == null || fileData.get(i).get(key).equals(value)))
				returnList.add(i);
		return returnList;
	}
	
	/**
	 * For internal testing purposes only
	 */
	public static void main(String[] args) {
		FileInput itemList = new FileInput("support/items.txt");
		for (int i = 0; i < itemList.size(); i++) {
			System.out.println(i + ": " + itemList.getRawDataAt(i));
		}
	}
}
